Imports TreeCreeper01Interfaces


'Canod: give an icon for leaf nodes

'is there a good built in way to get the file title from a filename string and use thatfor the caption instead of the fully qualified path?

Public Class fSpeciesTree

    Private pTree As ITaxonomicTree
    Private mLastHighlightedNode As TreeNode

    Public Sub DisplayTaxonomicTree(ByVal Study As IStudy, ByVal Title As String)

        Me.Text = Title

        If Not Study Is Nothing Then
            pTree = Study.TaxonomicTree
            DisplayTree()
        End If

    End Sub

    Private Sub DisplayTree()

        TheTree.BeginUpdate()
        With pTree
            AddTreeNodeRecursive(Nothing, .RootNode)
        End With
        TheTree.ShowNodeToolTips = True
        TheTree.Nodes.Item(0).ExpandAll()
        TheTree.EndUpdate()
        Show()

    End Sub

    Private Sub AddTreeNodeRecursive(ByVal VisualTreeParentNode As TreeNode, ByVal TaxonomicTreeNode As ITaxonomicTreeNode)

        Dim ChildIndex As Integer
        Dim VisualTreeNode As TreeNode
        Dim NumberOfSpecies As Integer

        If VisualTreeParentNode Is Nothing Then
            NumberOfSpecies = pTree.AllSpecies.Count
            VisualTreeNode = TheTree.Nodes.Add(TaxonomicTreeNode.UniqueKey, "All " & NumberOfSpecies & " species")
        Else
            VisualTreeNode = VisualTreeParentNode.Nodes.Add(TaxonomicTreeNode.UniqueKey, TaxonomicTreeNode.Name)
        End If
        VisualTreeNode.Tag = TaxonomicTreeNode


        With TaxonomicTreeNode
            If .ChildNodeCount > 0 Then
                For ChildIndex = 1 To .ChildNodeCount
                    AddTreeNodeRecursive(VisualTreeNode, .ChildNodeItemByIndexBaseOne(ChildIndex))
                Next
                VisualTreeNode.ForeColor = Color.Brown
            Else
                VisualTreeNode.ForeColor = Color.DarkOliveGreen
            End If
        End With

    End Sub

    Private Sub fSpeciesTree_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

        Const Border As Integer = 4

        With Me.TheTree
            .Left = Border
            .Top = Border
            .Height = ClientRectangle.Height - Border * 2
            .Width = ClientRectangle.Width - Border * 2
        End With

    End Sub

    Private Sub MouseInfo(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseHoverEventArgs) Handles TheTree.NodeMouseHover

        Dim ReportText As String
        Dim DoClear As Boolean
        Dim SelectedControlNode As TreeNode
        Dim MousedOverControlNode As TreeNode
        Dim SelectedTaxonomicNode As TreeCreeper01Interfaces.ITaxonomicTreeNode
        Dim MousedOverTaxonomicNode As TreeCreeper01Interfaces.ITaxonomicTreeNode
        Dim TaxonomicDistance As Single
        Dim TreeDistance As Integer

        MousedOverControlNode = e.Node
        If MousedOverControlNode Is Nothing Then
            ReportText = ""
            DoClear = True
        Else
            MousedOverTaxonomicNode = CType(MousedOverControlNode.Tag, ITaxonomicTreeNode)
            If MousedOverTaxonomicNode.ChildNodeCount > 0 Then
                ReportText = "The moused-over node is not a leaf"
                DoClear = True
            Else
                SelectedControlNode = TheTree.SelectedNode
                If SelectedControlNode Is Nothing Then
                    ReportText = "No species node is selected for comparison"
                    DoClear = True
                Else
                    If SelectedControlNode Is MousedOverControlNode Then
                        ReportText = ""
                        DoClear = True
                    Else
                        SelectedTaxonomicNode = CType(SelectedControlNode.Tag, ITaxonomicTreeNode)
                        If SelectedTaxonomicNode.ChildNodeCount > 0 Then
                            ReportText = "The node selected for comparison is not a leaf"
                            DoClear = True
                        Else
                            TaxonomicDistance = pTree.TaxonomicDistanceBetweenNodes(SelectedTaxonomicNode, MousedOverTaxonomicNode)
                            TreeDistance = pTree.LogicalDistanceBetweenNodes(SelectedTaxonomicNode, MousedOverTaxonomicNode)
                            ReportText = "Taxonomic distance between " & MousedOverTaxonomicNode.Name & " and " & SelectedTaxonomicNode.Name & ": " & TaxonomicDistance.ToString() & "  (tree distance is " & TreeDistance & ")."
                            HighlightNode(MousedOverControlNode)
                        End If
                    End If
                End If
            End If
        End If

        If DoClear Then
            ClearLastHighlightedNode()
        End If
        LabelReadoff.Text = ReportText

    End Sub

    Private Sub HighlightNode(ByVal Node As TreeNode)
        ClearLastHighlightedNode()
        mLastHighlightedNode = Node
        mLastHighlightedNode.BackColor = Color.LawnGreen
        mLastHighlightedNode.ForeColor = Color.Black
    End Sub

    Private Sub ClearLastHighlightedNode()
        If Not mLastHighlightedNode Is Nothing Then
            mLastHighlightedNode.BackColor = TheTree.BackColor
            mLastHighlightedNode.ForeColor = Color.DarkOliveGreen
        End If
    End Sub

    Private Sub fSpeciesTree_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class